//
//  BalanceViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/8.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "BalanceViewController.h"
#import "CashWithdrawalViewController.h"
#import "BuyBackPasswordViewController.h"

@interface BalanceViewController ()
@property (nonatomic, strong)UILabel *moneyLab;
@property (nonatomic, strong)NSString *allMoney;
@property (nonatomic, assign)BOOL isPass;

@end

@implementation BalanceViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestPasswordSettingParmers];
    [self requestMineWallet];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"余额";
    UIImageView *imageView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH/2-50*displaywidth, 60*displaywidth, 100*displaywidth, 100*displaywidth) ImageName:@"qianbao"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
    UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(50*displaywidth, 165*displaywidth, SCREEN_WIDTH-100*displaywidth, 20*displaywidth) Font:14.0 Text:@"我的余额" Color:RGB(77, 77, 77)];
    titLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titLab];
    self.moneyLab = [ZCControl createLabelWithFrame:CGRectMake(50*displaywidth, 200*displaywidth, SCREEN_WIDTH-100*displaywidth, 25*displaywidth) Font:18.0 Text:@"" Color:RGB(77, 77, 77)];
    _moneyLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_moneyLab];
    UIButton *drawalBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 250*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(drawalAction) Title:@"提现" Font:15.0];
    [drawalBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    drawalBtn.backgroundColor = ThemeColor;
    [drawalBtn.layer setMasksToBounds:YES];
    [drawalBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:drawalBtn];
}

//提现
- (void)drawalAction
{
    if (!_isPass)
    {
        [LTools showMBProgressWithText:@"请设置提现密码" addToView:sharedDelegate.window];
        BuyBackPasswordViewController *vc = [[BuyBackPasswordViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    if ([_allMoney floatValue] == 0)
    {
        [LTools showMBProgressWithText:@"余额不足，无法提现" addToView:self.view];
        return;
    }
    CashWithdrawalViewController *vc = [[CashWithdrawalViewController alloc] init];
    vc.myMoney = _allMoney;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestMineWallet
{
    WEAKSELF;
    [HYBaseHandler requestMineWalletParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSString *money = obj[@"data"][@"money"];
            weakSelf.allMoney = money;
            dispatch_async(dispatch_get_main_queue(), ^{
                double moneyD = [money doubleValue];
                weakSelf.moneyLab.text = [NSString stringWithFormat:@"最多可提现￥%.2lf",moneyD];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)requestPasswordSettingParmers
{
    WEAKSELF;
    [HYBaseHandler requestPasswordSettingParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSInteger passType = [obj[@"data"] integerValue];
            if (passType == 1)
            {
                weakSelf.isPass = YES;
            }
            else
            {
                weakSelf.isPass = NO;

            }
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
